import unittest

from tinydb.storage.entry import *

class TestStorage(unittest.TestCase):
    def test_table_tuple(self):
        actual = table_tuple_get_all('t1')
        expected = [
            (1,'小明'),
            (2,'小红'),
            (3,'小刚'),
            (4,'小丽')
        ]
        self.assertEqual(list(actual), expected)
    
    def test_index_tuple_get_equal_value(self):
        actual = index_tuple_get_equal_value('idx', (1,))
        expected = [(1, '小明')]
        self.assertEqual(list(actual), expected)

        actual = index_tuple_get_equal_value('idx', (2,))
        expected = [(2, '小红')]
        self.assertEqual(list(actual), expected)

        actual = index_tuple_get_equal_value('idx', (3,))
        expected = [(3, '小刚')]
        self.assertEqual(list(actual), expected)


    def test_index_tuple_get_range(self):
        actual = index_tuple_get_range('idx', (1,), (3,))
        expected = [(2, '小红')]
        self.assertEqual(list(actual), expected)

        actual = index_tuple_get_range('idx', (2,), (4,))
        expected = [(3, '小刚')]
        self.assertEqual(list(actual), expected)

        # 超过范围
        actual = index_tuple_get_range('idx', (4,), (6,))
        expected = []
        self.assertEqual(list(actual), expected)
        # 错误范围内没有数据
        actual = index_tuple_get_range('idx', (6,), (4,))
        expected = []
        self.assertEqual(list(actual), expected)

        # 只传 start
        actual = index_tuple_get_range('idx', (1,))
        expected = [(2, '小红'), (3, '小刚'), (4, '小丽')]
        self.assertEqual(list(actual), expected)

        # 只传 end
        actual = index_tuple_get_range('idx', None, (3,))
        expected = [(1, '小明'), (2, '小红')]
        self.assertEqual(list(actual), expected)

        # 不传 start 和 end
        actual = index_tuple_get_range('idx')
        expected = []
        self.assertEqual(list(actual), expected)

    def test_coverd_index_tuple(self):
        actual = covered_index_tuple_get_range('idx', (1,), (3,))
        expected = [(2,)]
        self.assertEqual(list(actual), expected)

        # 只传 start
        actual = covered_index_tuple_get_range('idx', (1,))
        expected = [(2,), (3,), (4,)]
        self.assertEqual(list(actual), expected)

        # 只传 end
        actual = covered_index_tuple_get_range('idx', None, (3,))
        expected = [(1,), (2,)]
        self.assertEqual(list(actual), expected)

        # 不传 start 和 end
        actual = covered_index_tuple_get_range('idx')
        expected = []
        self.assertEqual(list(actual), expected)

    def test_covered_index_tuple_get_equal_value(self):
        actual = covered_index_tuple_get_equal_value('idx', (1,))
        expected = [(1,)]
        self.assertEqual(list(actual), expected)
        
        actual = covered_index_tuple_get_equal_value('idx', (2,))
        expected = [(2,)]
        self.assertEqual(list(actual), expected)

        actual = covered_index_tuple_get_equal_value('idx', (3,))
        expected = [(3,)]
        self.assertEqual(list(actual), expected)

        actual = covered_index_tuple_get_equal_value('idx', (4,))
        expected = [(4,)]
        self.assertEqual(list(actual), expected)

    @unittest.expectedFailure
    def test_covered_index_tuple_get_equal_value_fail(self):
        actual = covered_index_tuple_get_equal_value('idx', (0,))
        expected = []
        self.assertEqual(list(actual), expected)
        actual = covered_index_tuple_get_equal_value('idx', (5,))
        expected = []
        self.assertEqual(list(actual), expected)
    

    @unittest.expectedFailure
    def test_index_tuple_get_equal_value_fail(self):
        actual = index_tuple_get_equal_value('idx', (0,))
        expected = []
        self.assertEqual(list(actual), expected)

if __name__ == '__main__':
    unittest.main()
